/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.beans.factory.config;

import java.beans.PropertyEditor;
import java.security.AccessControlContext;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.HierarchicalBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.core.convert.ConversionService;
import org.springframework.lang.Nullable;
import org.springframework.util.StringValueResolver;

/**
 * Configuration接口多数都是由工厂bean来实现的,他除了提供了{@link BeanFactory}中的客户端方法外,还额外提供了配置Bean的方法.
 * 这个Bean工厂接口的存在并不意味着我们在正常的应用上下文内可以使用他,尤其是{@link BeanFactory}
 * 和{@link org.springframework.beans.factory.ListableBeanFactory},
 * 提供这个扩展的目的仅仅只是为了在框架内部插件展示以及提供对Bean工厂配置方法的特殊访问.
 * 简而言之,就是提供了对工厂的操作,但是不建议我们使用.
 * <p>
 * Configuration interface to be implemented by most bean factories. Provides
 * facilities to configure a bean  , in addition to the bean factory
 * client methods in the {@link org.springframework.beans.factory.BeanFactory}
 * interface.
 * <p>
 * <p>This bean factory interface is not meant to be used in normal application
 * code: Stick to {@link org.springframework.beans.factory.BeanFactory} or
 * {@link org.springframework.beans.factory.ListableBeanFactory} for typical
 * needs. This extended interface is just meant to allow for framework-internal
 * plug'n'play and for special access to bean factory configuration methods.
 *
 * @author Juergen Hoeller
 * @see org.springframework.beans.factory.BeanFactory
 * @see org.springframework.beans.factory.ListableBeanFactory
 * @see ConfigurableListableBeanFactory
 * @since 03.11.2003
 */
public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry {

	/**
	 * 标准单例作用域的作用域标识符:singleton.
	 * 自定义范围可以通过registerScope添加.
	 * Scope identifier for the standard singleton scope: "singleton".
	 * Custom scopes can be added via {@code registerScope}.
	 *
	 * @see #registerScope
	 */
	String SCOPE_SINGLETON = "singleton";

	/**
	 * 原型模式作用域的作用域标识符:prototype.
	 * 自定义作用域可以通过registerScope方法来添加.
	 * Scope identifier for the standard prototype scope: "prototype".
	 * Custom scopes can be added via {@code registerScope}.
	 *
	 * @see #registerScope
	 */
	String SCOPE_PROTOTYPE = "prototype";


	/**
	 * 设置当前工厂的父工厂.
	 * 需要注意的是,副队相簿能改变:如果在工厂实例化时他不可用,他应该只在外部设置.
	 * Set the parent of this bean factory.
	 * <p>Note that the parent cannot be changed: It should only be set outside
	 * a constructor if it isn't available at the time of factory instantiation.
	 *
	 * @param parentBeanFactory the parent BeanFactory
	 * @throws IllegalStateException if this factory is already associated with
	 *                               a parent BeanFactory
	 * @see #getParentBeanFactory()
	 */
	void setParentBeanFactory(BeanFactory parentBeanFactory) throws IllegalStateException;

	/**
	 * 设置加载类的类加载器,
	 * 默认使用当前线程的类加载器,需要注意的是,这个类加载器仅适用于尚未包含已解析bean类的bean定义.
	 * 在spring2.0的情况下:bean定义只承载bean类的名称,一旦被工厂处理了,就会被解析.
	 * <p>
	 * Set the class loader to use for loading bean classes.
	 * Default is the thread context class loader.
	 * <p>Note that this class loader will only apply to bean definitions
	 * that do not carry a resolved bean class yet. This is the case as of
	 * Spring 2.0 by default: Bean definitions only carry bean class names,
	 * to be resolved once the factory processes the bean definition.
	 *
	 * @param beanClassLoader the class loader to use,
	 *                        or {@code null} to suggest the default class loader
	 */
	void setBeanClassLoader(@Nullable ClassLoader beanClassLoader);

	/**
	 * Return this factory's class loader for loading bean classes.
	 */
	@Nullable
	ClassLoader getBeanClassLoader();

	/**
	 * 指定一个临时的类加载器用于类型匹配.
	 * 默认是不存在的,只需要使用标准的类加载器,一个临时类加载器通常只在运行时交织(运行时动态修改字节码)时才被指定,以确保实际的bean类被尽可能的
	 * 延迟加载.
	 * 一旦BeanFactory完成其引导阶段,临时加载器将被移除.
	 * <p>
	 * <p>
	 * Specify a temporary ClassLoader to use for type matching purposes.
	 * Default is none, simply using the standard bean ClassLoader.
	 * <p>A temporary ClassLoader is usually just specified if
	 * <i>load-time weaving</i> is involved, to make sure that actual bean
	 * classes are loaded as lazily as possible. The temporary loader is
	 * then removed once the BeanFactory completes its bootstrap phase.
	 *
	 * @since 2.5
	 */
	void setTempClassLoader(@Nullable ClassLoader tempClassLoader);

	/**
	 * 返回一个用于类型匹配的临时类加载器.
	 * Return the temporary ClassLoader to use for type matching purposes,
	 * if any.
	 *
	 * @since 2.5
	 */
	@Nullable
	ClassLoader getTempClassLoader();

	/**
	 * 设置是否换缓存bean元数据,如给定bean定义(以合并的方式)和已解析的bean类.
	 * 默认开启,关闭这个标志来启用bean定义对象的热刷新,如果这个标志关闭了,任何bean实例的创建都将重新查询bean类加载器来查找新解析的类.
	 * <p>
	 * <p>
	 * Set whether to cache bean metadata such as given bean definitions
	 * (in merged fashion) and resolved bean classes. Default is on.
	 * <p>Turn this flag off to enable hot-refreshing of bean definition objects
	 * and in particular bean classes. If this flag is off, any creation of a bean
	 * instance will re-query the bean class loader for newly resolved classes.
	 */
	void setCacheBeanMetadata(boolean cacheBeanMetadata);

	/**
	 * 返回是否启用了缓存bean元数据,在给定的bean定义(合并的方式)解析bean类.
	 * <p>
	 * <p>
	 * Return whether to cache bean metadata such as given bean definitions
	 * (in merged fashion) and resolved bean classes.
	 */
	boolean isCacheBeanMetadata();

	/**
	 * 为bean定义值中的表达式指定解析策略.
	 * 在bean工厂中,默认的情况下是没有活动的表达式支持的.
	 * 应用程序上下文中通常会设置一个标准的表达式策略,比如UnifiedEl兼容风格里支持'#{...}'的格式.
	 * <p>
	 * <p>
	 * Specify the resolution strategy for expressions in bean definition values.
	 * <p>There is no expression support active in a BeanFactory by default.
	 * An ApplicationContext will typically set a standard expression strategy
	 * here, supporting "#{...}" expressions in a Unified EL compatible style.
	 *
	 * @since 3.0
	 */
	void setBeanExpressionResolver(@Nullable BeanExpressionResolver resolver);

	/**
	 * 获取bean定义值中表达式的解析策略.
	 * Return the resolution strategy for expressions in bean definition values.
	 *
	 * @since 3.0
	 */
	@Nullable
	BeanExpressionResolver getBeanExpressionResolver();

	/**
	 * 指定一个Spring3.0 ConversionService用于转换属性值,作为JavaBeanPropertyEditors的替代.
	 * <p>
	 * <p>
	 * Specify a Spring 3.0 ConversionService to use for converting
	 * property values, as an alternative to JavaBeans PropertyEditors.
	 *
	 * @since 3.0
	 */
	void setConversionService(@Nullable ConversionService conversionService);

	/**
	 * 返回关联的ConversionService(如果有的情况下).
	 * <p>
	 * <p>
	 * Return the associated ConversionService, if any.
	 *
	 * @since 3.0
	 */
	@Nullable
	ConversionService getConversionService();

	/**
	 * 添加一个PropertyEditorRegistrar以应用于所有的bean创建过程.
	 * 这种注册操作会为每一个bean的创建都创建一个新的PropertyEditor,用来注册他们到指定的注册表里.
	 * 这种方式避免了自定义编辑器的同步需求,因此,最好使用这种方法,而不是使用registerCustomEditor方法.
	 * <p>
	 * <p>
	 * Add a PropertyEditorRegistrar to be applied to all bean creation processes.
	 * <p>Such a registrar creates new PropertyEditor instances and registers them
	 * on the given registry, fresh for each bean creation attempt. This avoids
	 * the need for synchronization on custom editors; hence, it is generally
	 * preferable to use this method instead of {@link #registerCustomEditor}.
	 *
	 * @param registrar the PropertyEditorRegistrar to register
	 */
	void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar);

	/**
	 * 为指定类型的所有属性注册一个指定的自定义的属性编辑器,其将会在工程配置期间被调用.
	 * 需要注意的是,这个方法注册的是一个共享的自定义编辑器实例:
	 * 所以访问者该实例最好是一个线程安全的有锁实现.通常最好使用addPropertyEditorRegistrar来代替该方法,来避免需要在自定义编辑上进行同步.
	 * <p>
	 * <p>
	 * Register the given custom property editor for all properties of the
	 * given type. To be invoked during factory configuration.
	 * <p>Note that this method will register a shared custom editor instance;
	 * access to that instance will be synchronized for thread-safety. It is
	 * generally preferable to use {@link #addPropertyEditorRegistrar} instead
	 * of this method, to avoid for the need for synchronization on custom editors.
	 *
	 * @param requiredType        type of the property
	 * @param propertyEditorClass the {@link PropertyEditor} class to register
	 */
	void registerCustomEditor(Class<?> requiredType, Class<? extends PropertyEditor> propertyEditorClass);

	/**
	 * 使用已经在此BeanFactory中注册的自定义编辑器初始化指定的PropertyEditorRegistry.
	 * <p>
	 * <p>
	 * Initialize the given PropertyEditorRegistry with the custom editors
	 * that have been registered with this BeanFactory.
	 *
	 * @param registry the PropertyEditorRegistry to initialize
	 */
	void copyRegisteredEditorsTo(PropertyEditorRegistry registry);

	/**
	 * 给当前BeanFactory设置一个自定义的类型转换器,用来转换Bean的属性值构造参数等值.
	 * 他将覆盖掉默认的PropertyEditor机制,从而使任何自定义编辑器或者自定义编辑器互不相关.
	 * <p>
	 * <p>
	 * Set a custom type converter that this BeanFactory should use for converting
	 * bean property values, constructor argument values, etc.
	 * <p>This will override the default PropertyEditor mechanism and hence make
	 * any custom editors or custom editor registrars irrelevant.
	 *
	 * @see #addPropertyEditorRegistrar
	 * @see #registerCustomEditor
	 * @since 2.5
	 */
	void setTypeConverter(TypeConverter typeConverter);

	/**
	 * 获取当前Bean工厂的转换器,他在每次调用的时候都应该刷新一个新实例,因为这个类型转换器通常不是线程安全的.
	 * 如果激活了默认的PropertyEditor机制,将会返回所有的已经注册的自定义的编辑器.
	 * <p>
	 * <p>
	 * Obtain a type converter as used by this BeanFactory. This may be a fresh
	 * instance for each call, since TypeConverters are usually <i>not</i> thread-safe.
	 * <p>If the default PropertyEditor mechanism is active, the returned
	 * TypeConverter will be aware of all custom editors that have been registered.
	 *
	 * @since 2.5
	 */
	TypeConverter getTypeConverter();

	/**
	 * 给注释值添加一个字符串解析器
	 * Add a String resolver for embedded values such as annotation attributes.
	 *
	 * @param valueResolver the String resolver to apply to embedded values
	 * @since 3.0
	 */
	void addEmbeddedValueResolver(StringValueResolver valueResolver);

	/**
	 * 当前工厂中是否已经注册了一个用于resolveEmbeddedValue的注解值解析器.
	 * Determine whether an embedded value resolver has been registered with this
	 * bean factory, to be applied through {@link #resolveEmbeddedValue(String)}.
	 *
	 * @since 4.3
	 */
	boolean hasEmbeddedValueResolver();

	/**
	 * 解析给定的注解值,比如一个注解属性
	 * Resolve the given embedded value, e.g. an annotation attribute.
	 *
	 * @param value the value to resolve
	 * @return the resolved value (may be the original value as-is)
	 * @since 3.0
	 */
	@Nullable
	String resolveEmbeddedValue(String value);

	/**
	 * 添加一个bean后处理器,在工厂配置期间,他将被应用于这个工厂创建的bean.
	 * 需要注意的是,这些添加的后置处理器会按照他们被注册的顺序来执行,通过Ordered接口表达的排序语义都会被忽略.
	 * 哪些自动检测的bean后处理器,使用会在以编程方式注册后应用.
	 * Add a new BeanPostProcessor that will get applied to beans created
	 * by this factory. To be invoked during factory configuration.
	 * <p>Note: Post-processors submitted here will be applied in the order of
	 * registration; any ordering semantics expressed through implementing the
	 * {@link org.springframework.core.Ordered} interface will be ignored. Note
	 * that autodetected post-processors (e.g. as beans in an ApplicationContext)
	 * will always be applied after programmatically registered ones.
	 *
	 * @param beanPostProcessor the post-processor to register
	 */
	void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);

	/**
	 * 返回已经注册的bean后处理器的数量.
	 * Return the current number of registered BeanPostProcessors, if any.
	 */
	int getBeanPostProcessorCount();

	/**
	 * 注册给定的范围,由给定的范围实现支持.
	 * <p>
	 * Register the given scope, backed by the given Scope implementation.
	 *
	 * @param scopeName the scope identifier
	 * @param scope     the backing Scope implementation
	 */
	void registerScope(String scopeName, Scope scope);

	/**
	 * 返回当前注册的作用域的名称集合.(只会返回显示注册的作用域,像内部的单例,原型之类的不会被返回.)
	 * <p>
	 * Return the names of all currently registered scopes.
	 * <p>This will only return the names of explicitly registered scopes.
	 * Built-in scopes such as "singleton" and "prototype" won't be exposed.
	 *
	 * @return the array of scope names, or an empty array if none
	 * @see #registerScope
	 */
	String[] getRegisteredScopeNames();

	/**
	 * 在显示注册的作用域内查找指定的作用域(不包括内置的单例,原型作用域).
	 * <p>
	 * <p>
	 * Return the Scope implementation for the given scope name, if any.
	 * <p>This will only return explicitly registered scopes.
	 * Built-in scopes such as "singleton" and "prototype" won't be exposed.
	 *
	 * @param scopeName the name of the scope
	 * @return the registered Scope implementation, or {@code null} if none
	 * @see #registerScope
	 */
	@Nullable
	Scope getRegisteredScope(String scopeName);

	/**
	 * 提供与该工厂相关的安全访问控制上下文.
	 * Provides a security access control context relevant to this factory.
	 *
	 * @return the applicable AccessControlContext (never {@code null})
	 * @since 3.0
	 */
	AccessControlContext getAccessControlContext();

	/**
	 * 复制指定工厂中所有相关配置.
	 * 应该包括:所有的标准配置,Bean后处理器,作用域,工厂内部的设置.
	 * 不应该包括:实际bean定义的任何元数据,比如bean定义对象或者bean名称的别名.
	 * Copy all relevant configuration from the given other factory.
	 * <p>Should include all standard configuration settings as well as
	 * BeanPostProcessors, Scopes, and factory-specific internal settings.
	 * Should not include any metadata of actual bean definitions,
	 * such as BeanDefinition objects and bean name aliases.
	 *
	 * @param otherFactory the other BeanFactory to copy from
	 */
	void copyConfigurationFrom(ConfigurableBeanFactory otherFactory);

	/**
	 * 给指定的bean名称创建一个别名,我们通常使用该方法来支持在XML中ID内非法的名称.
	 * 通常在工厂配置期间调用,但是也可以用于在运行时注册别名,因此工厂实现应将该方法设为同步的.
	 * <p>
	 * <p>
	 * Given a bean name, create an alias. We typically use this method to
	 * support names that are illegal within XML ids (used for bean names).
	 * <p>Typically invoked during factory configuration, but can also be
	 * used for runtime registration of aliases. Therefore, a factory
	 * implementation should synchronize alias access.
	 *
	 * @param beanName the canonical name of the target bean
	 * @param alias    the alias to be registered for the bean
	 * @throws BeanDefinitionStoreException if the alias is already in use
	 */
	void registerAlias(String beanName, String alias) throws BeanDefinitionStoreException;

	/**
	 * 在当前工厂中,使用指定字符值解析器来解析别名所有的别名及其对应的名称.
	 * 值解析器可以解析目标Bean名称中的占位符甚至是别名.
	 * <p>
	 * <p>
	 * Resolve all alias target names and aliases registered in this
	 * factory, applying the given StringValueResolver to them.
	 * <p>The value resolver may for example resolve placeholders
	 * in target bean names and even in alias names.
	 *
	 * @param valueResolver the StringValueResolver to apply
	 * @since 2.5
	 */
	void resolveAliases(StringValueResolver valueResolver);

	/**
	 * 返回指定bean名称合并后的bean定义,如果需要的话会合并子bean定义和父bean定义.
	 * 当然也会考虑祖先工厂中的bean定义.
	 * Return a merged BeanDefinition for the given bean name,
	 * merging a child bean definition with its parent if necessary.
	 * Considers bean definitions in ancestor factories as well.
	 *
	 * @param beanName the name of the bean to retrieve the merged definition for
	 * @return a (potentially merged) BeanDefinition for the given bean
	 * @throws NoSuchBeanDefinitionException if there is no bean definition with the given name
	 * @since 2.5
	 */
	BeanDefinition getMergedBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

	/**
	 * 确定指定名称的bean是不是工厂bean.
	 * Determine whether the bean with the given name is a FactoryBean.
	 *
	 * @param name the name of the bean to check
	 * @return whether the bean is a FactoryBean
	 * ({@code false} means the bean exists but is not a FactoryBean)
	 * @throws NoSuchBeanDefinitionException if there is no bean with the given name
	 * @since 2.5
	 */
	boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException;

	/**
	 * 显示的控制指定bean的当前创建状态,仅供容器内部使用.
	 * Explicitly control the current in-creation status of the specified bean.
	 * For container-internal use only.
	 *
	 * @param beanName   the name of the bean
	 * @param inCreation whether the bean is currently in creation
	 * @since 3.1
	 */
	void setCurrentlyInCreation(String beanName, boolean inCreation);

	/**
	 * 确定指定的bean是否正在创建.
	 * Determine whether the specified bean is currently in creation.
	 *
	 * @param beanName the name of the bean
	 * @return whether the bean is currently in creation
	 * @since 2.5
	 */
	boolean isCurrentlyInCreation(String beanName);

	/**
	 * 给指定的bean注册一个依赖bean.在给定的bean销毁之前需要被销毁.
	 * <p>
	 * <p>
	 * Register a dependent bean for the given bean,
	 * to be destroyed before the given bean is destroyed.
	 *
	 * @param beanName          the name of the bean
	 * @param dependentBeanName the name of the dependent bean
	 * @since 2.5
	 */
	void registerDependentBean(String beanName, String dependentBeanName);

	/**
	 * 返回依赖指定bean的所有bean的名称.
	 * Return the names of all beans which depend on the specified bean, if any.
	 *
	 * @param beanName the name of the bean
	 * @return the array of dependent bean names, or an empty array if none
	 * @since 2.5
	 */
	String[] getDependentBeans(String beanName);


	/**
	 * 返回指定bean所以来的所有bean.
	 * <p>
	 * <p>
	 * Return the names of all beans that the specified bean depends on, if any.
	 *
	 * @param beanName the name of the bean
	 * @return the array of names of beans which the bean depends on,
	 * or an empty array if none
	 * @since 2.5
	 */
	String[] getDependenciesForBean(String beanName);

	/**
	 * 根据一个bean定义,销毁指定的bean实例,通常是从该工厂获取的原型实例.
	 * 在销毁期间出现的任何异常都应该被捕获并记录,而不是传递给该方法的调用者.
	 * <p>
	 * <p>
	 * Destroy the given bean instance (usually a prototype instance
	 * obtained from this factory) according to its bean definition.
	 * <p>Any exception that arises during destruction should be caught
	 * and logged instead of propagated to the caller of this method.
	 *
	 * @param beanName     the name of the bean definition
	 * @param beanInstance the bean instance to destroy
	 */
	void destroyBean(String beanName, Object beanInstance);

	/**
	 * 销毁当前目标作用域中指定的作用域bean.
	 * 在销毁期间出现的任何异常应该被捕获并记录,而不是传播给这个方法的调用者.
	 * <p>
	 * <p>
	 * Destroy the specified scoped bean in the current target scope, if any.
	 * <p>Any exception that arises during destruction should be caught
	 * and logged instead of propagated to the caller of this method.
	 *
	 * @param beanName the name of the scoped bean
	 */
	void destroyScopedBean(String beanName);

	/**
	 * 销毁该工厂中所有的单例bean,包括已注册的一次性bean(该方法通常在工厂关闭时调用).
	 * 在销毁期间出现的任何应该被捕获并记录,而不是传播给这个方法的调用者.
	 * <p>
	 * <p>
	 * Destroy all singleton beans in this factory, including inner beans that have
	 * been registered as disposable. To be called on shutdown of a factory.
	 * <p>Any exception that arises during destruction should be caught
	 * and logged instead of propagated to the caller of this method.
	 */
	void destroySingletons();

}
